package cfm.com.cfmscreen;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.speech.RecognitionListener;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.speech.tts.TextToSpeech;
import android.support.annotation.RequiresApi;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cfm.com.cfmscreen.comm.MyApp;
import cfm.com.cfmscreen.comm.Url;
import cfm.com.cfmscreen.comm.UserInfo;
import cfm.com.cfmscreen.comm.Utils;

@SuppressLint("NewApi")
public class LoginActivity extends Activity implements View.OnClickListener {

    Button loginBtn = null;
    EditText useridEt = null;
    EditText passEt = null;
    TextView promptText = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(this);
        useridEt = (EditText) findViewById(R.id.userId);
        passEt = (EditText) findViewById(R.id.pass);
        promptText = (TextView) findViewById(R.id.promptText);

    }

    @Override
    public void onClick(View v) {

        if(checkEdit()){
            login();
        }
    }


    private boolean checkEdit(){

        if(useridEt.getText().toString().trim().equals("")){
            Toast.makeText(LoginActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
        }else if(passEt.getText().toString().trim().equals("")){
            Toast.makeText(LoginActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
        }else{
            return true;
        }
        return false;
    }

    private void login(){

        String httpUrl = Url.baseUrl+Url.login;
        final Map<String, String> map = new HashMap<String, String>();
        map.put("userName", useridEt.getText().toString().trim());
        map.put("passWord", passEt.getText().toString().trim());
        StringRequest post = new StringRequest(Request.Method.POST, httpUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject obj = new JSONObject(s);
                    String state = obj.getString("state");
                    if(state.equals("success")){
                        JSONObject object = (JSONObject) obj.get("user");
                        UserInfo userInfo = new UserInfo();
                        userInfo.setUserId(object.getInt("userId"));
                        userInfo.setUserName(useridEt.getText().toString().trim());
                        userInfo.setHospital(object.getString("hospitalName"));
                        userInfo.setHospitalId(object.getInt("hospitalId"));
                        userInfo.setAreaId(object.getInt("patientAreaId"));
                        UserInfo.setInstance(userInfo);

                        Intent intent2 = new Intent(LoginActivity.this,MainActivity.class);
                        startActivity(intent2);
                    }else{
                        Toast.makeText(LoginActivity.this, state, Toast.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {

                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return map;
            }
        };
        MyApp.requestQueue.add(post);
    }

    @Override
    protected void onResume() {
        super.onResume();
        System.out.println("onResume333====");

        if (MyApp.mWebSocketClient != null) {
            MyApp.mWebSocketClient.close();
            MyApp.mWebSocketClient=null;
        }
    }

}
